//
//  TextNoteLabel.m
//  IconMaster
//
//  Created by 为为 on 13-6-8.
//  Copyright (c) 2013年 为为. All rights reserved.
//

#import "TextNoteLabel.h"

#define		TEXT_VIEW_TAG		1000
#define		TEXT_VIEW_DELEGAET_TAG 2000

@interface TextViewDelegate : UIView<UITextViewDelegate> {
	
}
- (void)labelTapped;
@end

@implementation TextViewDelegate

- (void)labelTapped {
	if (self.superview && [self.superview isKindOfClass:[UILabel class]]) {
		UIView* textView = [self.superview viewWithTag:TEXT_VIEW_TAG];
		
		if (textView) {
			if ([textView isFirstResponder]) {
				[textView resignFirstResponder];
			} else {
				[textView becomeFirstResponder];
			}
		}
	}
}

- (void)textViewDidChange:(UITextView *)textView {
	if (textView.superview && [textView.superview isKindOfClass:[UILabel class]]) {
		UILabel* label = (UILabel*)textView.superview;
		
		label.text = textView.text;
		
		if ([[label.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) {
			return;
		}
		
		UIFont *font;
		CGSize size;
		
		do {
			font = [label.font fontWithSize:label.font.pointSize+1];
			label.font = font;
			size = [label.text sizeWithFont:font
						  constrainedToSize:CGSizeMake(label.frame.size.width, 2000)
							  lineBreakMode:NSLineBreakByCharWrapping];
		} while (size.height < label.frame.size.height);
		
		do {
			if (label.font.pointSize < 1) {
				break;
			}
			
			font = [label.font fontWithSize:label.font.pointSize-1];
			label.font = font;
			size = [label.text sizeWithFont:font
						  constrainedToSize:CGSizeMake(label.frame.size.width, 2000)
							  lineBreakMode:NSLineBreakByCharWrapping];
		} while (size.height > label.frame.size.height);
	}
}
@end

@implementation TextNoteLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
		self.autoresizingMask = 63;
		self.font = [self.font fontWithSize:200];
//		self.minimumFontSize = 1;
        self.minimumScaleFactor = 1/[UIFont labelFontSize];
		self.adjustsFontSizeToFitWidth = TRUE;
		self.numberOfLines = 0;
		self.userInteractionEnabled = FALSE;
		self.textAlignment = NSTextAlignmentCenter;
		self.lineBreakMode = NSLineBreakByCharWrapping;
		self.backgroundColor = [UIColor clearColor];
		
		TextViewDelegate* tvd = [[TextViewDelegate alloc] initWithFrame:CGRectZero];
		tvd.tag = TEXT_VIEW_DELEGAET_TAG;
		[self addSubview:tvd];
		
		UIGestureRecognizer* gr = [[UITapGestureRecognizer alloc] initWithTarget:tvd action:@selector(labelTapped)];
		[self addGestureRecognizer:gr];
		
		UITextView* textView = [[UITextView alloc] initWithFrame:CGRectZero];
		textView.autocorrectionType = UITextAutocorrectionTypeNo;
		textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
		textView.tag = TEXT_VIEW_TAG;
		textView.delegate = tvd;
		[self addSubview:textView];
		
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)resizeSelf
{
	UITextView* tv = (UITextView*)[self viewWithTag:TEXT_VIEW_TAG];
	TextViewDelegate* tvd = (TextViewDelegate*)[self viewWithTag:TEXT_VIEW_DELEGAET_TAG];
	[tvd textViewDidChange:tv];
}

- (void)showKeyboard
{
	UITextView* textView = (UITextView*)[self viewWithTag:TEXT_VIEW_TAG];
	[textView becomeFirstResponder];
}

- (void)hideKeyboard
{
	UITextView* textView = (UITextView*)[self viewWithTag:TEXT_VIEW_TAG];
	[textView resignFirstResponder];
}

- (BOOL)isKeyboardShown
{
	UITextView* textView = (UITextView*)[self viewWithTag:TEXT_VIEW_TAG];
	if ([textView isFirstResponder]) {
		return true;
	} else {
		return false;
	}
}

@end
